package com.qf.j2112.service.impl;

import com.qf.j2112.po.SysCart;
import com.qf.j2112.service.SysCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qf.j2112.mapper.SysCartMapper;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 购物车Service业务层处理
 * @author j2112
 * @date 2022-04-12
 */

@Service
public class SysCartServiceImpl implements SysCartService {

    @Resource
    private SysCartMapper sysCartMapper;

    /**
     * 查询用户购物车列表
     * @param userId 用户主键
     * @return 购物车管理
     */
    @Override
    public List<SysCart> selectSysCartList(Integer userId)
    {
        return sysCartMapper.selectSysCartListByUserId(userId);
    }

    /**
     * 查询用户购物车长度
     * @param userId 用户id
     * @return 购物车管理集合
     */
    @Override
    public int selectSysCartNumByUserId(Integer userId){
        return sysCartMapper.selectSysCartNumByUserId(userId);
    }
    /**
     * 添加商品到购物车
     * @param sysCart 购物车
     * @return 结果
     */
    @Override
    public boolean insertSysCart(SysCart sysCart) {
        SysCart sysCart1=sysCartMapper.selectSysCartByUserIdAndGoodsId(sysCart.getCartUserId(),sysCart.getCartGoodsId());
       if(ObjectUtils.isEmpty(sysCart1)){
           int i=sysCartMapper.insertSysCart(sysCart);
           if(i>-1){
               return true;
           }
       }else {
           //原来的数量+1
           Integer newNum=sysCart1.getCartGoodsNum()+sysCart.getCartGoodsNum();
           sysCart1.setCartGoodsNum(newNum);
           int i=sysCartMapper.updateSysCart(sysCart1);
           if(i>-1){
               return true;
           }
       }
        return false;
    }

    /**
     * 修改购物车
     * @param sysCart 购物车管理
     * @return 结果
     */
    @Override
    public int updateSysCart(SysCart sysCart) {
        return sysCartMapper.updateSysCart(sysCart);
    }


    /**
     * 删除购物车管理信息
     * @param cartId 购物车管理主键
     * @return 结果
     */
    @Override
    public int deleteSysCartByCartId(Integer cartId)
    {
        return sysCartMapper.deleteSysCartByCartId(cartId);
    }

    @Override
    public int deleteSysCartByUserId(Integer userId) {
        return sysCartMapper.deleteSysCartByUserId(userId);
    }
}
